﻿package app.view.mediator
{	
    import app.facade.App; 
	import app.view.component.Loading;
	import app.view.component.moment.Module;
	import flash.events.Event;
	import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.mediator.Mediator;
	import org.osflash.thunderbolt.Logger;
    
    /**
     * @author hungconcon
     */
    public class LoadingMediator extends Mediator implements IMediator {
		
        public static const NAME:String = "LoadingMediator";	
        public function LoadingMediator( viewComponent:Object ) { 
			super( NAME, viewComponent ); 
		}
		protected function get ui():Loading { 
			return viewComponent as Loading; 
		}
		override public function listNotificationInterests():Array {
            return [
					App.LOADING_BUFFERING,
					App.LOADING_DONE,
					App.LOADING_SHOW,
					App.LOADING_HIDE
					];
        }
        override public function handleNotification( note:INotification ):void {
			var name:String = note.getName();
			var value:Object = note.getBody();
			switch(name) {
				case App.LOADING_BUFFERING: 
					ui.setPercent(Number(value));
				break;
				case App.LOADING_DONE: 
					ui.setHide();
					ui.setReset();
				break;
				case App.LOADING_SHOW: 
					ui.setShow();
				break;
				case App.LOADING_HIDE: 
					ui.setHide();
				break;
			}
        }
		override public function onRegister():void {
			super.onRegister();
			ui.addEventHandler(App.LOADING_DONE, 	 eventHandlers);
			ui.addEventHandler(App.LOADING_SHOW, 	 eventHandlers);
			ui.addEventHandler(App.LOADING_HIDE, 	 eventHandlers);
			ui.addEventHandler(App.LOADING_BUFFERING, eventHandlers);
		}
		private function eventHandlers( event:Event ):void {
			switch( event.type ) {
				case App.LOADING_DONE: 
					sendNotification(App.LOADING_DONE);
				break;
				case App.LOADING_SHOW: 
					sendNotification(App.LOADING_SHOW);
				break;
				case App.LOADING_HIDE: 
					sendNotification(App.LOADING_HIDE);
				break;
				case App.LOADING_BUFFERING: 
					sendNotification(App.LOADING_BUFFERING, event.currentTarget.value);
				break;
				default: break;
			}
		}
		
    }
}